package wendyeq.iweb.blog.dao.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;


import wendyeq.iweb.blog.dao.IArticleDAO;
import wendyeq.iweb.blog.pojo.Article;
import wendyeq.iweb.blog.util.DateUtil;
import wendyeq.iweb.blog.util.JdoManager;


@Repository("articleDAO")
public class ArticleDAO implements IArticleDAO{
	private static final Logger logger = Logger.getLogger(ArticleDAO.class);
	
	@Override
	public boolean saveOrUpdate(Article article) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			manager.makePersistent(article);
			logger.info("a"+article.getId());
		} catch (Exception e) {
			logger.error(e);
			return false;
		} finally{
			manager.close();
		}
		return true;
	}
	/***
	 * 用于批量修改和增加
	 * 
	 * */
	public Collection<Article> saveOrUPdatebatch(List<Article> al){
		PersistenceManager manager = JdoManager.getSession();
		Collection<Article> ca=null;
		try {
			ca=manager.makePersistentAll(al);
		} catch (Exception e) {
			logger.error(e);
			return ca;
		} finally{
			manager.close();
		}
		return null;
	}
	
	@Override
	public boolean delete(String id) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			Article article = manager.getObjectById(Article.class, id);
			manager.deletePersistent(article);
		} catch (Exception e) {
			logger.error(e);
			return false;
		} finally{
			manager.close();
		}
		return true;
	}
	@Override
	public Article findById(String id) {
		Article article = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			article = manager.getObjectById(Article.class, id);
		} catch (Exception e) {
			logger.error(e);
		}finally{
			manager.close();
		}
		return article;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> findAll() {
		List<Article> articles = null;
		PersistenceManager manager = JdoManager.getSession();
		Query query = manager.newQuery(Article.class);
		query.setOrdering("createdTime DESC");
		try {
			
			articles = (List<Article>) query.execute();
			logger.warn("articles-size: " + articles.size());
		} catch (Exception e) {
			logger.error(e);
		}finally{
			query.closeAll();
			manager.close();
		}
		return articles;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Article> findByPage(int min, int max) {
		List<Article> articles = null;
		PersistenceManager manager = JdoManager.getSession();
		Query query = manager.newQuery(Article.class);
		query.setOrdering("createdTime DESC");
		query.setRange(min, max);
		try {
			articles = (List<Article>) query.execute();
			logger.warn("articles-size: " + articles.size());
		} catch (Exception e) {
			logger.error(e);
		}finally{
			query.closeAll();
			manager.close();
		}
		return articles;
	}

	@Override
	public Article findByAlias(String a, Date time) {
		List<Article> articles = null;
		logger.warn(a);
		PersistenceManager manager = JdoManager.getSession();
		Query query = manager.newQuery(Article.class);
		query.setFilter("alias == a");
		query.declareParameters("String a");
		try {
			articles = (List<Article>) query.execute(a);
			logger.warn("article size " + articles.size());
		} catch (Exception e) {
			logger.error(e);
		}finally{
			query.closeAll();
			manager.close();
		}
		if (articles.size() == 1) {
			return articles.get(0);
		} else {
			return null;
		}
	}
	
	@Override
	public List<Article> findByCat(int min, int max, String cat) {
		List<Article> articles = null;
		PersistenceManager manager = JdoManager.getSession();
		Query query = manager.newQuery(Article.class);
		query.setFilter("category == cat");
		query.setOrdering("createdTime DESC");
		query.setRange(min, max);
		query.declareParameters("String cat");
		try {
			articles = (List<Article>) query.execute(cat);
		} catch (Exception e) {
			logger.error(e);
		}finally{
			query.closeAll();
			manager.close();
		}
		return articles;
	}

	@Override
	public List<Article> findByArchive(int min,int max,Date date) {
		List<Article> articles = null;
		PersistenceManager manager = JdoManager.getSession();
		Query query = manager.newQuery(Article.class);
		query.setFilter("createdTime >= date");
		query.declareParameters("java.util.Date date");
		query.setOrdering("createdTime DESC");
		query.setRange(min, max);
		try {
		
			articles = (List<Article>) query.execute(date);
			logger.warn("article date " + articles.size());
		} catch (Exception e) {
			logger.error(e);
		}finally{
			query.closeAll();
			manager.close();
		}
		return articles;

	}

	public Article findByTitle(String t, Date time) {
		List<Article> articles = null;
		logger.warn(t);
		PersistenceManager manager = JdoManager.getSession();
		Query query = manager.newQuery(Article.class);
		query.setFilter("title == t");
		query.declareParameters("String t");
		try {
			articles = (List<Article>) query.execute(t);
			logger.warn("article size " + articles.size());
		} catch (Exception e) {
			logger.error(e);
		}finally{
			query.closeAll();
			manager.close();
		}
		if (articles.size() == 1) {
			return articles.get(0);
		} else {
			return null;
		}

	}
	@Override
	public List<Article> findByDateCategory(String category, Date date) {
		List<Article> articles = null;
		PersistenceManager manager = JdoManager.getSession();
		Query query = manager.newQuery(Article.class);
		query.setFilter("(createdTime <date2 && createdTime > date1) && category==cate");
		query.declareParameters("java.util.Date date1, java.util.Date date2 ,String cate");
		query.setOrdering("createdTime desc");
		try {
			articles = (List<Article>) query.execute(DateUtil.getNow(),DateUtil.getEnd(),category);
			logger.info("********************* " + articles.size());
		} catch (Exception e) {
			logger.info(e);
		}finally{
			query.closeAll();
			manager.close();
		}
		return articles;
	}
	
}